﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Homework8_Order;

namespace Homework8
{
    public partial class Form1 : Form
    {
        OrderService service = new OrderService();
        public Form2 form2;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

            Order firstOrder = new Order(1, "小黑");
            OrderItem apple1 = new OrderItem(5, 10, "苹果");
            OrderItem peer1 = new OrderItem(4, 5, "梨");
            firstOrder.addOrderItem(apple1);
            firstOrder.addOrderItem(peer1);

            Order secondOrder = new Order(2, "小红");
            OrderItem apple2 = new OrderItem(5, 10, "苹果");
            OrderItem peer2 = new OrderItem(4, 5, "梨");
            secondOrder.addOrderItem(apple2);
            secondOrder.addOrderItem(peer2);

            //Order thirdOrder = new Order(2, "小红");

            service.addOrder(firstOrder);
            service.addOrder(secondOrder);


        }
        //查询
        private void search_btn_Click(object sender, EventArgs e)
        {
            try
            {
                orderBindingSource.DataSource = service.OrderList.Where(order => order.OrderID == Convert.ToInt32(textBox1_1.Text));
            }
            catch (System.FormatException)
            {
                orderBindingSource.DataSource = service.OrderList.Where(order => order.Name == textBox1_1.Text);
            }
            finally
            {
                Console.WriteLine("请输入正确的客户名或者订单号");
            }


        }
        //删除
        private void delete_btn_Click(object sender, EventArgs e)
        {
            service.deleteOrder(Convert.ToInt32(textBox1_1.Text));
            orderBindingSource.ResetBindings(true);
        }
        //添加订单
        private void add_btn_Click(object sender, EventArgs e)
        {
            Form f2 = new Form2(this); 
            f2.ShowDialog();
            Order newOrder = new Order(Convert.ToInt32(form2.textBox1.Text), form2.textBox2.Text);
            service.addOrder(newOrder);
            OrderItem newItem = new OrderItem(Convert.ToInt32(form2.textBox3.Text), Convert.ToInt32(form2.textBox4.Text), form2.textBox5.Text);
            newOrder.addOrderItem(newItem);
            orderBindingSource.ResetBindings(true);
        }
        //修改订单
        private void Alter_btn_Click(object sender, EventArgs e)
        {
            Form f2 = new Form2(this);
            Order newOrder = service.searchOrder(Convert.ToInt32(textBox1_1.Text));
            form2.textBox1.Text = Convert.ToString(newOrder.OrderID);
            form2.textBox2.Text = newOrder.Name;
            OrderItem newItem = newOrder.orderList.First();
            form2.textBox3.Text = Convert.ToString(newItem.Price);
            form2.textBox4.Text = Convert.ToString(newItem.Amount);
            form2.textBox5.Text = newItem.GoodsName;
            f2.ShowDialog();
            newOrder.OrderID = Convert.ToInt32(form2.textBox1.Text);
            newOrder.Name = form2.textBox2.Text;
            newItem.Price = Convert.ToInt32(form2.textBox3.Text);
            newItem.Amount = Convert.ToInt32(form2.textBox4.Text);
            newItem.GoodsName = form2.textBox5.Text;
        }
        //导出订单
        private void export_btn_Click(object sender, EventArgs e)
        {
            service.Export(textBox1_2.Text);
        }
        //导入订单
        private void import_btn_Click(object sender, EventArgs e)
        {
            service.Import(textBox1_2.Text);
            orderBindingSource.ResetBindings(true);
        }


    }
}
